package com.lmx.repoDomain.repository;

import com.lmx.common.constants.CommonConstants;
import com.lmx.persistence.service.CommonService;
import com.lmx.persistence.service.JsonModelService;
import com.lmx.persistence.service.NursingService;
import com.lmx.persistence.service.RegisterService;
import com.lmx.persistence.service.UserService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.JsonModel;
import com.lmx.repoDomain.tmodel.NursingPlan;
import com.lmx.repoDomain.tmodel.NursingPlanServiceItem;
import com.lmx.repoDomain.tmodel.NursingProjectServiceItem;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.internal.compiler.lookup.InferenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@JsonApiResourceRepository(JsonModel.class)
@Component
public class JsonModelRepository extends BaseRepository<JsonModel> {
	@Autowired
	private NursingService nursingService;

	@Autowired
	private CommonService commonService;

	@Autowired
	private UserService userService;

	@Autowired
	private JsonModelService jsonModelService;

	@Override
	@JsonApiSave
	public JsonModel save(JsonModel jsonModel) {
		String type = jsonModel.getType();
		String jsonData = jsonModel.getJsonData();
		if(type != null && "roombeds".equals(type)){
			jsonData="["+jsonData+"]";
		}
		JSONArray jsonArray = JSONArray.fromObject(jsonData);
		int flag = 0;  
		String insertSql = "";
		String insertData = "";
		if (type != null && "nursingplanitem".equals(type)) {// 批量插入plan所对应projectitem
			if (jsonArray.size() > 0) {
				JSONObject jsonObject = jsonArray.getJSONObject(0);
				Long planId = jsonObject.getLong("plan");
				nursingService.deleteNursingPlanServiceItem(planId);
			}
			insertSql = "insert tnb_nursingplan_serviceitem (serviceitem_id,nursingplan_id,del_status,starttime_tab,week_tab) values ";
			for (int i = 0; i < jsonArray.size(); i++) {
				// String insertData;
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				if (jsonObject.getString("item") == null || "".equals(jsonObject.getString("item"))) {

				} else {
					flag++;
					insertData += "," + "(" + jsonObject.getLong("item") + "," + jsonObject.getLong("plan") + "," + 0
							+ "," + jsonObject.getInt("startTimeTab") + "," + jsonObject.getInt("weekTab") + ")";
				}
			}
		}

		if (type != null && "staffcustomer".equals(type)) {// 批量插入staff所对应customer
			if (jsonArray.size() > 0) {
				JSONObject jsonObject = jsonArray.getJSONObject(0);
				Long staffId = jsonObject.getLong("staff");
				userService.deleteStaffCustomer(staffId);
			}
			insertSql = "insert tnb_staff_customer (staff_id,customer_id,del_status) values ";
			for (int i = 0; i < jsonArray.size(); i++) {
				// String insertData;
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				if (jsonObject.getString("customer") == null || "".equals(jsonObject.getString("customer"))) {

				} else {
					flag++;
					insertData += "," + "(" + jsonObject.getLong("staff") + "," + jsonObject.getLong("customer") + ","
							+ 0 + ")";
				}
			}
		}

		if (type != null && "roombeds".equals(type)) {// 批量生成room以及bed
			if (jsonArray.size() > 0) {
				String createFlag = jsonModelService.createRoomsBeds(jsonData);
				if (!"1".equals(createFlag)) { 
					jsonModel.setErrcode(14);// 添加失败
					throw new BusinessException(null, CommonConstants.ERROR_CODE.ADD_ERROR.label()+"-"+createFlag, null);
				}
			}
		}

		if (flag > 0) {
			insertSql += insertData.substring(1, insertData.length());
			commonService.patchInsertItems(insertSql);
		}
		return null;
	}

}
